package br.mg.bhe.ftpobre.util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

/**
 * 
 * @author Aureliano
 */
public final class LogUtil {

	private final static Logger logger = Logger.getLogger(LogUtil.class.getName());
	
	private LogUtil() {
		super();
	}
	
	public static Logger getLogger(Class<?> clazz) {
		Logger l = Logger.getLogger(clazz.getName());
		l.addHandler(getHandler());
		
		return l;
	}
	
	public static FileHandler getFileAppender() {
		return getHandler();
	}
    
    private static FileHandler getHandler() {
        try {
        	return new FileHandler(getLogFileName(), true);
        } catch (IOException ex) {
			logger.severe(ex.getMessage());
			throw new RuntimeException(ex);
		}
    }

    private static String getLogFileName() throws IOException {
        Date now = new Date(System.currentTimeMillis());
        return LocalFileUtil.getLogFolder() +
                "/ftpobre-log_" + new SimpleDateFormat("dd-MM-yyyy").format(now) + ".log";
    }
}